package org.itboys.mobile.controller.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.product.SundayProductDto;
import org.itboys.mobile.service.mysql.weixin.SundayWeiXinProductService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/8/24
 * 描述：产品controller
 */
@Controller
@RequestMapping("/sunday/small/product")
@Api(value = "product",description = "楼盘")
public class SundaySmallProductController extends BaseController{
    @Autowired
    private SundayWeiXinProductService productService;

    @RequestMapping(value="/select", method= {RequestMethod.POST})
    @ApiOperation(value="获取楼盘列表。分页")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "type", value = "类型 1.搜楼列表  2.优选列表") @RequestParam(value = "type", required = true) Integer type,
            @ApiParam(required = false, name = "isRecommend", value = "是否推荐") @RequestParam(value = "isRecommend", required = false) Integer isIndexRecommend,
            @ApiParam(required = false, name = "parentId", value = "市Id") @RequestParam(value = "parentId", required = false) Long parentId,
            @ApiParam(required = false, name = "categoryId", value = "区域Id") @RequestParam(value = "categoryId", required = false) Long categoryId,
            @ApiParam(required = false, name = "isHot", value = "是否爆款") @RequestParam(value = "isHot", required = false) Integer isHot,
            @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
            @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
            @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空。") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(required = false, name = "order", value = "排序参数。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order
            ){
        try{
            List<SundayProductDto> result=productService.selectProduct(request,type);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/small/product/select----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/findOne", method = {RequestMethod.POST})
    @ApiOperation(value = "获取楼盘详情。code!=0直接提示消息")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "楼盘ID") @RequestParam(value = "productId", required = true) Long productId,
            @ApiParam(value = "用户ID") @RequestParam(value = "memberId", required = false) Long memberId
            ) {
        try{
            SundayProductDto result = productService.findOne(productId,memberId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/small/product/findOne----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
}
